<?php
// +----------------------------------------------------------------------
// | Site:  [ http://www.yzmcms.com]
// +----------------------------------------------------------------------
// | Copyright: 袁志蒙工作室，并保留所有权利
// +----------------------------------------------------------------------
// | Author: YuanZhiMeng <214243830@qq.com>
// +---------------------------------------------------------------------- 
// | Explain: 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用，不允许对程序代码以任何形式任何目的的再发布！
// +----------------------------------------------------------------------

defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_sys_class('page','',0);
yzm_base::load_common('class/jst.class.php');
yzm_base::load_common('class/xlswriter.class.php');

class jst_orders extends common {

	/**
	 * 配置信息
	 */

	public $config=[];
	public $data=[];
	public $jst=[];
	public $jst_arr=[];
	
	public function __construct(){
		$this->jst = get_jst_list();
		$k = 0;
		foreach($this->jst as $v){
			$this->jst_arr[$k]['id'] = $v['id'];
			$this->jst_arr[$k]['title'] = $v['short_name'];
			$k++;
		}
		parent::__construct();
	}
	
	public function order_list() {
		$of = input('post.field') ? input('post.field') : 'modified';
		$or = input('post.order') ? input('post.order') : 'desc';
		$download = isset($_POST['download']) ? $_POST['download'] : 0;
		$page = input('post.page');
		$limit = input('post.limit');
		$d = D('jst_orders');
		$where = '1=1';
		$i_id =$_POST["i_id"] ?? '';
		$sku_id = $_POST["sku_id"] ?? '';
		$o_id = $_POST["o_id"] ?? '';
		$shop_id = isset($_POST["shop_id"]) ? $_POST["shop_id"] : '';
		$start = isset($_POST["start"]) ? $_POST["start"] : date("Y-m-d",strtotime("-7 day"));
		$end = isset($_POST["end"]) ? $_POST["end"] : date('Y-m-d');
		$time_type = $_POST["time_type"] ?? 0;
		$properties_value = isset($_POST['properties_value']) ? safe_replace(trim($_POST['properties_value'])) : '';
		$name = isset($_POST["name"]) ? $_POST["name"] : '';
		if($i_id) {
			if(strpos($i_id,',')){
				$i_id = str_replace(',','","',$i_id);
				$where .= ' AND i_id in("'.$i_id.'")';
			}else{
				if($i_id != trim($i_id,'*')){
					$i_id = trim($i_id,'*');
					$where .= ' AND i_id LIKE \'%'.$i_id.'%\'';
				}else{
					$where .= ' AND i_id = "'.$i_id.'"';
				}
			}
		}
		if($sku_id) {
			if(strpos($sku_id,',')){
				$sku_id = str_replace(',','","',$sku_id);
				$where .= ' AND sku_id in("'.$sku_id.'")';
			}else{
				if($sku_id != trim($sku_id,'*')){
					$sku_id = trim($sku_id,'*');
					$where .= ' AND sku_id LIKE \'%'.$sku_id.'%\'';
				}else{
					$where .= ' AND sku_id = "'.$sku_id.'"';
				}
			}
		}
		
		if($shop_id){
			$shop_id = str_replace(',','","',$shop_id);
			$where .= ' AND shop_id in("'.$shop_id.'")';
		}
		//订单时间
		if($time_type == 0 && $start && $end){
			$where .= ' AND order_date >='.strtotime($start).' AND order_date<'.(strtotime($end)+86400);
		}
		//发货时间
		if($time_type == 1 && $start && $end){
			$where .= ' AND send_date >='.strtotime($start).' AND send_date<'.(strtotime($end)+86400);
		}
		if($properties_value) {
			$where .= ' AND properties_value LIKE \'%'.$properties_value.'%\'';
		}
		if($name) {
			$where .= ' AND name LIKE \'%'.$name.'%\'';
		}
		if($o_id) {
			$where .= ' AND o_id ='.$o_id;
		}
		$shop = get_shop_all();
		if($page&&$download==0){
			$total = $d->where($where)->group('o_id')->total();
			$qty_sum = $d->field('sum(qty) qty_sum')->where($where)->one();
			$page = new page($total, $limit);
			$data = $d->field('*,sum(qty) qty_sum')->order("$of $or")->where($where)->limit($page->limit())->order("$of $or")->group('o_id')->select();
			foreach($data as &$v){
				$v['jst_name'] = $this->jst[$v['jst_id']]['short_name'];
				$v['shop_name'] = '';
				if($v['shop_id']>0){
					$v['shop_name'] = $shop[$v['shop_id']]['nick'];
				}
				$v['status'] = get_orders_status($v['status_code']);
			}
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$data,'qty_sum'=>$qty_sum?$qty_sum:0];
			return_json($data);
		}
		if($download==1){
			$number = 50000;
			$total = $qty_sum = $d->field('sum(qty) qty_sum')->where($where)->one();
			if($total>50000){
				$data = ['code'=>1,'msg'=>'单次最多导出5万条记录','url'=>$url];
				return_json($data);
			}
			$page = ceil($total/$number);
			$excel = new xlswriter('orders',1);
			for($i=1;$i<=$page;$i++){
				$limit = ($i-1)*$number.','.$number;
				$data = $d
					->field('id,o_id,so_id,order_date,send_date,remark,labels,status_code,sku_id,properties_value,qty,name,shop_id')
					->where($where)
					->order("$of $or")
					->limit($limit)
					->select();
				foreach($data as &$v){
					$v['shop_name'] = '';
					if($v['shop_id']>0){
						$v['shop_name'] = $shop[$v['shop_id']]['nick'];
					}
					$v['status'] = get_orders_status($v['status_code']);
				}
				//var_dump($data);exit;
				//setcache('data_'.$i,$data);
				$excel->insert_data($data);
				unset($data);
			}
			$url = $excel->output();
			$data = ['code'=>0,'msg'=>'','url'=>$url];
			return_json($data);
		}else{
			include $this->admin_tpl('jst_orders/order_list');
		}
	}
	
	//订单明细
	public function orders_items(){
		$page = input('get.page');
		if($page){
			if(!$_GET['o_id']&&$_GET['shop_id']){
				return;
			}
			$of = input('get.of');
			$or = input('get.or');
			$of = $of ? $of : 'id';
			$or = $or ? $or : 'ASC';
			$d = D('jst_orders');
			$where = '1=1 AND o_id='.$_GET['o_id'].' AND shop_id = '.$_GET['shop_id'];
			$total = $d->where($where)->total();
			$qty_sum = $d->field('sum(qty) qty_sum')->where($where)->one();
			$page = new page($total, 100);
			$data = $d->field('id,i_id,sku_id,qty,name,pic,properties_value')->order("$of $or")->where($where)->limit($page->limit())->order("$of $or")->select();
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$data,'qty_sum'=>$qty_sum?$qty_sum:0];
			return_json($data);
		}
		include $this->admin_tpl('jst_orders/order_item');


	}
	
	//操作记录
	public function order_action(){
		$page = input('get.page');
		if($page){
			$jst_id = input('get.jst_id');
			$o_id = input('get.o_id');
			$jst = new jst($jst_id);
			$arr = [
				'o_id' => $_GET['o_id'],
				"page_index" => 1,
				"page_size" => 300
			];
			$result = $jst->order_action($arr);
			$data = $result['data']['datas'];
			$data = ['code'=>0,'msg'=>'','data'=>$data];
			return_json($data);
		}
		include $this->admin_tpl('jst_orders/order_action');
	}
}